package visitor

import (
	"github.com/flipped-aurora/gin-vue-admin/server/global"
	"github.com/flipped-aurora/gin-vue-admin/server/model/common/request"
	"github.com/flipped-aurora/gin-vue-admin/server/model/common/response"
	"github.com/flipped-aurora/gin-vue-admin/server/model/visitor"
	visitorReq "github.com/flipped-aurora/gin-vue-admin/server/model/visitor/request"
	"github.com/flipped-aurora/gin-vue-admin/server/service"
	"github.com/gin-gonic/gin"
	"go.uber.org/zap"
)

type VisitApi struct {
}

var visitService = service.ServiceGroupApp.VisitorServiceGroup.VisitService

// CreateVisit 创建visit表
// @Tags Visit
// @Summary 创建visit表
// @Security ApiKeyAuth
// @accept application/json
// @Produce application/json
// @Param data body visitor.Visit true "创建visit表"
// @Success 200 {string} string "{"success":true,"data":{},"msg":"创建成功"}"
// @Router /visit/createVisit [post]
func (visitApi *VisitApi) CreateVisit(c *gin.Context) {
	var visit visitor.Visit
	err := c.ShouldBindJSON(&visit)
	if err != nil {
		response.FailWithMessage(err.Error(), c)
		return
	}
	if err := visitService.CreateVisit(&visit); err != nil {
		global.GVA_LOG.Error("创建失败!", zap.Error(err))
		response.FailWithMessage("创建失败", c)
	} else {
		response.OkWithMessage("创建成功", c)
	}
}

// DeleteVisit 删除visit表
// @Tags Visit
// @Summary 删除visit表
// @Security ApiKeyAuth
// @accept application/json
// @Produce application/json
// @Param data body visitor.Visit true "删除visit表"
// @Success 200 {string} string "{"success":true,"data":{},"msg":"删除成功"}"
// @Router /visit/deleteVisit [delete]
func (visitApi *VisitApi) DeleteVisit(c *gin.Context) {
	var visit visitor.Visit
	err := c.ShouldBindJSON(&visit)
	if err != nil {
		response.FailWithMessage(err.Error(), c)
		return
	}
	if err := visitService.DeleteVisit(visit); err != nil {
		global.GVA_LOG.Error("删除失败!", zap.Error(err))
		response.FailWithMessage("删除失败", c)
	} else {
		response.OkWithMessage("删除成功", c)
	}
}

// DeleteVisitByIds 批量删除visit表
// @Tags Visit
// @Summary 批量删除visit表
// @Security ApiKeyAuth
// @accept application/json
// @Produce application/json
// @Param data body request.IdsReq true "批量删除visit表"
// @Success 200 {string} string "{"success":true,"data":{},"msg":"批量删除成功"}"
// @Router /visit/deleteVisitByIds [delete]
func (visitApi *VisitApi) DeleteVisitByIds(c *gin.Context) {
	var IDS request.IdsReq
	err := c.ShouldBindJSON(&IDS)
	if err != nil {
		response.FailWithMessage(err.Error(), c)
		return
	}
	if err := visitService.DeleteVisitByIds(IDS); err != nil {
		global.GVA_LOG.Error("批量删除失败!", zap.Error(err))
		response.FailWithMessage("批量删除失败", c)
	} else {
		response.OkWithMessage("批量删除成功", c)
	}
}

// UpdateVisit 更新visit表
// @Tags Visit
// @Summary 更新visit表
// @Security ApiKeyAuth
// @accept application/json
// @Produce application/json
// @Param data body visitor.Visit true "更新visit表"
// @Success 200 {string} string "{"success":true,"data":{},"msg":"更新成功"}"
// @Router /visit/updateVisit [put]
func (visitApi *VisitApi) UpdateVisit(c *gin.Context) {
	var visit visitor.Visit
	err := c.ShouldBindJSON(&visit)
	if err != nil {
		response.FailWithMessage(err.Error(), c)
		return
	}
	if err := visitService.UpdateVisit(visit); err != nil {
		global.GVA_LOG.Error("更新失败!", zap.Error(err))
		response.FailWithMessage("更新失败", c)
	} else {
		response.OkWithMessage("更新成功", c)
	}
}

// FindVisit 用id查询visit表
// @Tags Visit
// @Summary 用id查询visit表
// @Security ApiKeyAuth
// @accept application/json
// @Produce application/json
// @Param data query visitor.Visit true "用id查询visit表"
// @Success 200 {string} string "{"success":true,"data":{},"msg":"查询成功"}"
// @Router /visit/findVisit [get]
func (visitApi *VisitApi) FindVisit(c *gin.Context) {
	var visit visitor.Visit
	err := c.ShouldBindQuery(&visit)
	if err != nil {
		response.FailWithMessage(err.Error(), c)
		return
	}
	if revisit, err := visitService.GetVisit(visit.ID); err != nil {
		global.GVA_LOG.Error("查询失败!", zap.Error(err))
		response.FailWithMessage("查询失败", c)
	} else {
		response.OkWithData(gin.H{"revisit": revisit}, c)
	}
}

// GetVisitList 分页获取visit表列表
// @Tags Visit
// @Summary 分页获取visit表列表
// @Security ApiKeyAuth
// @accept application/json
// @Produce application/json
// @Param data query visitorReq.VisitSearch true "分页获取visit表列表"
// @Success 200 {string} string "{"success":true,"data":{},"msg":"获取成功"}"
// @Router /visit/getVisitList [get]
func (visitApi *VisitApi) GetVisitList(c *gin.Context) {
	var pageInfo visitorReq.VisitSearch
	err := c.ShouldBindQuery(&pageInfo)
	if err != nil {
		response.FailWithMessage(err.Error(), c)
		return
	}
	if list, total, err := visitService.GetVisitInfoList(pageInfo); err != nil {
		global.GVA_LOG.Error("获取失败!", zap.Error(err))
		response.FailWithMessage("获取失败", c)
	} else {
		response.OkWithDetailed(response.PageResult{
			List:     list,
			Total:    total,
			Page:     pageInfo.Page,
			PageSize: pageInfo.PageSize,
		}, "获取成功", c)
	}
}
